import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 库存组织
 * @date: 2018-7-5
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryInventoryData, insertInventoryData as _insertInventoryData, updateInventoryData as _updateInventoryData, disableInventory, enableInventory, updateAllInventoryData as _updateAllInventoryData } from "../services/InventoryService";
export default {
  // 库存组织model
  namespace: 'inventoryOrg',
  state: {
    fetchInventoryData: {
      content: [],
      pagination: {}
    }
  },
  effects: {
    // 查询库存组织，获取库存组织数据
    fetchInventoryData: /*#__PURE__*/_regeneratorRuntime.mark(function fetchInventoryData(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchInventoryData$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryInventoryData, payload);

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateStateReducer',
                payload: {
                  fetchInventoryData: list,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchInventoryData);
    }),
    // 新建库存组织
    insertInventoryData: /*#__PURE__*/_regeneratorRuntime.mark(function insertInventoryData(_ref3, _ref4) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function insertInventoryData$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_insertInventoryData, payload);

            case 4:
              res = _context2.sent;
              return _context2.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, insertInventoryData);
    }),
    // 更新库存组织
    updateInventoryData: /*#__PURE__*/_regeneratorRuntime.mark(function updateInventoryData(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateInventoryData$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_updateInventoryData, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, updateInventoryData);
    }),
    // 批量新建、编辑库存组织
    updateAllInventoryData: /*#__PURE__*/_regeneratorRuntime.mark(function updateAllInventoryData(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateAllInventoryData$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_updateAllInventoryData, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateAllInventoryData);
    }),
    // 禁用库存组织（此方法暂不单用，禁用与编辑一起）
    disbledInventory: /*#__PURE__*/_regeneratorRuntime.mark(function disbledInventory(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function disbledInventory$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(disableInventory, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, disbledInventory);
    }),
    // 启用库存组织（此方法暂不单用，启用与编辑一起）
    enabledInventory: /*#__PURE__*/_regeneratorRuntime.mark(function enabledInventory(_ref11, _ref12) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function enabledInventory$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context6.next = 4;
              return call(enableInventory, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, enabledInventory);
    })
  },
  reducers: {
    // 用于合并state,并且返回一个新的state
    updateStateReducer: function updateStateReducer(state, _ref13) {
      var payload = _ref13.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    editData: function editData(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        fetchInventoryData: {
          content: action.payload.content
        }
      });
    }
  }
};